// packageG/mycomponent/assist-competition/index.js
let app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    U_tabcontrol: {
      type: null
    },
    bottomShowBolIndex: {
      type: null
    },
    index: {
      type: null
    },
    bottomShowBol: {
      type: null
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    goods: null,
    activeSpec: 0, // 选择的规格
    fieldStatus: 0,
    field: null,
    diyStyle: null
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.getData();
      this.setData({
        diyStyle: `
          --sku_item_active_color: ${this.data.datas.sku_item_active_color};
          --sku_item_active_bgcolor: ${this.data.datas.sku_item_active_bgcolor};
          --sku_item_color: ${this.data.datas.sku_item_color};
          --sku_item_bg_color: ${this.data.datas.sku_item_bg_color};
        `
      });
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.signup.get-signup-diyform');
      app._getNetWork({
        url: urlStr,
        data: {
          activity_id: this.data.datas.form.id
        },
        success: (resdata)=> {
          let { data } = resdata;
          if (data.result == 0) return app.tips(data.msg);
          let setData = {};
          setData.goods = data?.data?.goods;
          setData.field = data?.data?.diyform?.fields;
          if (setData.goods && setData.goods.has_option && setData.goods.has_many_options.length) {
            setData.activeSpec = setData.goods.has_many_options[0].id;
          }
          this.setData(setData);
        },
        fail: function(res) {
          console.log(res);
        }
      });
      
    },
    onSelectSku(event) {
      let {
        id
      } = event.currentTarget.dataset;
      this.setData({
        activeSpec: id
      });
    },

    submit(e) {
      if (e) this.data.form_data = e.detail;
      this.applyCompetition();
    },
    submitSign() {
      if (Object.keys(this.data.field).length != 0 && !this.data.form_data) {
        let childComponent = this.selectComponent('#diyform');
        childComponent.submit();
        return;
      }
      this.applyCompetition();
    },

    async applyCompetition() {
      if (this.lockSubmit) return;
      this.lockSubmit = true;

      let _type = this.data.goods ? "pay" : "signup"; // goods有值，需要走支付下单
      let _url = _type == "pay" ? "plugin.assist-competition.frontend.create" : "plugin.assist-competition.frontend.signup.signup-action";
      let assembleGoods = [{
        goods_id: this.data.goods?.id,
        total: 1,
        option_id: this.data.activeSpec
      }];
      let _data = {
        activity_id: this.data.datas.form.id,
        form_data: this.data.form_data[0],
        dispatch_type_id: 0
      };
      if (_type == "pay") {
        _data.goods = JSON.stringify(assembleGoods);
      }

      let urlStr = app.getNetAddresss(_url);
      let response = await app._postNetWork({
        url: urlStr,
        data: _data
      });
      let { result, msg, data } = response.data;

      this.lockSubmit = false;
      if (result == 0) return app.tips(msg);
      if (data.order_ids) {
        wx.redirectTo({
          url: "/packageD/buy/orderPay/orderPay?order_id=" + data.order_ids + "&status=2"
        });
        return
      }

      wx.showModal({
        content: msg,
        showCancel: false
      });
    }
  }
})